﻿using System;
using System.Collections.Generic;
using System.Web;
using Gama.Contract;
using Microsoft.ApplicationBlocks.Data;

using System.Data; 
namespace Gama.Service
{
    public class AccountService:IAccountService 
    {
        public bool AddOrUpdate(AccountDto dto)
        {                        
            return SqlService.ExecuteNonQueryWithoutResult("procAccountAddOrUpdate", dto.Id, dto.UserName, new SecurityService().Encrypt(dto.Password), dto.FirstName, dto.LastName, dto.Sex, dto.BirthDay, dto.ProvinceId, dto.Company, dto.Address, dto.Phone, dto.Tel, dto.Fax, dto.IsActived, dto.CreatedDate, dto.ModifiedDate);
        }
        public bool Active(Guid accountId)
        {
            return SqlService.ExecuteNonQueryWithoutResult("procAccountActive", accountId, DateTime.Now);
        }
        public bool CheckExist(string userName)
        {
            return SqlService.ExecuteNonQueryWithoutResult("procAccountCheckExists", userName);
        }
        public AccountDto Get(Guid id)
        {
            var iDataReader = SqlService.ExecuteNonQuery("procAccountGetbyId", id);
            return iDataReader.ConvertToDto<AccountDto>();
        }
        public AccountDto Get(string userName)
        {
            var iDataReader = (IDataReader)SqlHelper.ExecuteReader(Ultilities.ConnectionString, "procAccountGetbyUserName", userName);
            return iDataReader.ConvertToDto<AccountDto>();
        }
        public bool Delete(int id)
        {            
            return Convert.ToBoolean(SqlHelper.ExecuteScalar(Ultilities.ConnectionString, "procAccountDelete", id));
        }
        
        public IList<AccountDto> GetAll()
        {
            return SqlService.ExecuteNonQuery("procAccountGetAll",null).ToList<AccountDto>();
        }
        public static IList<ProvinceDto> GetProvince()
        {
            return SqlService.ExecuteNonQuery("procProvinceGetAll", null).ToList<ProvinceDto>();
        }

        #region "Guest"
        public bool GuestCheckExist(Guid id)
        {
            return SqlService.ExecuteNonQueryWithoutResult("procGuestCheckExists", id);
        }
        public GuestDto AddGuest()
        {
            var guest = new GuestDto
            {
                Id = Guid.NewGuid(),
                Ip = HttpContext.Current.Request.UserHostAddress,
                CreatedDate = DateTime.Now
            };
            return this.AddGuest(guest);
        }
        public GuestDto AddGuest(GuestDto guestDto)
        {
            SqlService .ExecuteScalar("procGuestAdd", guestDto.Id, guestDto.Ip, guestDto.CreatedDate);
            return guestDto;
        }
        public GuestDto GetGuest(Guid id)
        {
            var iDataReader = SqlService .ExecuteNonQuery("procGuestGetbyID", id);
            return iDataReader.ConvertToDto<GuestDto>();
        }
        #endregion
    }
}
